﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using VO.Business.Controllers;
using VO.Entities;

namespace VO.Web.Account
{
    public partial class Register : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {
            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);

            string continueUrl = RegisterUser.ContinueDestinationPageUrl;
            if (String.IsNullOrEmpty(continueUrl))
            {
                continueUrl = "~/";
            }
            
            //add automatically created user to Users Role
            Roles.AddUserToRole(RegisterUser.UserName, "Users");

            //create user in VO
            User newUser = new User();
            MembershipUser mUser = Membership.GetUser(RegisterUser.UserName);
            newUser.IdMembership = new Guid(mUser.ProviderUserKey.ToString());
            newUser.UserDescription = string.Empty;
            newUser.PublicName = mUser.UserName;
            UsersController.SaveUser(newUser, true);

            Response.Redirect(continueUrl);
        }

    }
}
